home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / Spectacle ƒ / Main.c next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  11.1 KB  |  597 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Main.c
  3.  *
  4.  *    Robert Dierkes,  November 11, 1993
  5.  *
  6.  *    Change History:
  7.  *
  8.  *       11/93    ???        New
  9.  *       4/96        bob        Updated #includes to support changed GX Library names.
  10.  *                        Changed boolean to Boolean.
  11.  *                        Added the copyright info.
  12.  *
  13.  *
  14.  *        © Apple Computer, Inc. 1990 - 1996  All rights reserved
  15.  *
  16.  */
  17.  
  18. /*------------------*/
  19. /*    Include Files    */
  20. /*------------------*/
  21. #include    <GestaltEqu.h>
  22. #include    <QuickDraw.h>
  23. #include    <Types.h>
  24. #include    <Fonts.h>
  25. #include    <Windows.h>
  26. #include    <Memory.h>
  27. #include    <LowMem.h>
  28. #include    <Dialogs.h>
  29.  
  30. #define debugging
  31.  
  32. #include    <GXEnvironment.h>
  33. #include    "GraphicsLibraries.h"
  34. #include    <GXErrors.h>
  35.  
  36. #include    "Object.h"
  37. #include    "ViewPorts.h"
  38. #include    "Main.h"
  39.  
  40.  
  41. /*----------------------*/
  42. /*    Global Declarations    */
  43. /*----------------------*/
  44. gxGraphicsClient gClient;
  45. Boolean            gQuitApp;
  46. Handle            ghMainMenu;
  47. Rect            gDragRect;
  48. WindowPtr        gWindow;
  49. Boolean            gDoAnimation,
  50.                 gCyclePanes;
  51. long            gNextCycle;
  52.  
  53.  
  54. /*------------------------------*/
  55. /*    External Declarations     */
  56. /*------------------------------*/
  57. extern    Boolean    gIsSquare;
  58. extern    Boolean    gShowFrame;
  59. extern    Boolean    gDoDither;
  60. extern    Boolean    gDoHalftone;
  61. extern    long    gPanesPerSide;
  62.  
  63.  
  64. long GetNextCycle (long *pNextCycle);
  65.     long
  66. GetNextCycle (long *pNextCycle)
  67. {
  68.     long    now;
  69.  
  70.     if ((now = TickCount ()) > *pNextCycle)
  71.     {
  72.         *pNextCycle = now + kCycleLength;
  73.         return (true);
  74.     }
  75.     else
  76.         return (false);
  77.  
  78. }    /* GetNextCycle */
  79.  
  80.  
  81.     Boolean
  82. InitApp (void)
  83. {
  84.     long            response;
  85.     gxGraphicsError    grfxError;
  86.  
  87.     if (Gestalt (gestaltGraphicsVersion, &response)) {
  88.         DebugStr ("\pQuickDraw GX is not installed");
  89.         return false;
  90.     }
  91.  
  92.     if ((gClient = GXNewGraphicsClient (nil, kGraphicsHeapSize, 0)) == 0) {
  93.         DebugStr ("\pNewGraphicsClient failed");
  94.         return false;
  95.     }
  96.  
  97.     /* Initialize Skia */
  98.     GXEnterGraphics ();
  99.  
  100.     GXSetValidation (gxNoValidation);
  101.     SetGraphicsLibraryErrors ();
  102.     SetGraphicsLibraryNotices();
  103.     InitCommonColors ();
  104.  
  105.     GXIgnoreGraphicsNotice (disposed_dead_caches);
  106.  
  107.     MaxApplZone();
  108.     MoreMasters();
  109.     MoreMasters();
  110.  
  111.     InitGraf (&qd.thePort);
  112.     InitFonts ();
  113.     FlushEvents (everyEvent, 0);
  114.     InitWindows ();
  115.     InitMenus ();
  116.     InitDialogs (nil);
  117.     InitCursor ();
  118.  
  119.     return true;
  120.  
  121. }    /* InitApp */
  122.  
  123.  
  124.     void
  125. ExitApp (void)
  126. {
  127.     if (gWindow)
  128.         DisposeWindow (gWindow);
  129.  
  130.        DisposeCommonColors ();
  131.  
  132.        GXPopGraphicsNotice (/*disposed_dead_caches*/);
  133.  
  134.     GXExitGraphics ();
  135.     if (gClient)
  136.         GXDisposeGraphicsClient (gClient);
  137.  
  138. }    /* ExitApp */
  139.  
  140.  
  141.     void
  142. InitializeMenus (void)
  143. {
  144.     ghMainMenu = GetNewMBar (menuBarRsrcID);
  145.     SetMenuBar (ghMainMenu);
  146.     AddResMenu (GetMHandle (appleMenuRsrcID), 'DRVR');
  147.     DrawMenuBar ();
  148.  
  149.     if (gDoAnimation)    EnableItem  (GetMHandle (windowMenuRsrcID), itemCycle);
  150.     else                DisableItem (GetMHandle (windowMenuRsrcID), itemCycle);
  151.     CheckItem (GetMHandle (objectMenuRsrcID), gIsSquare ? itemSquare : itemOval, true);
  152.     CheckItem (GetMHandle (objectMenuRsrcID), itemDither, gDoDither);
  153.     CheckItem (GetMHandle (objectMenuRsrcID), itemHalftone, gDoHalftone);
  154.     CheckItem (GetMHandle (windowMenuRsrcID), itemShowFrame, gShowFrame);
  155.     CheckItem (GetMHandle (windowMenuRsrcID), itemCycle, gCyclePanes);
  156.     CheckItem (GetMHandle (windowMenuRsrcID), gPanesPerSide - kMinimumPanesPerSide + itemMinimumPanes, true);
  157.  
  158. }    /* InitializeMenus */
  159.  
  160.  
  161.     WindowPtr
  162. InitializeWindow (void)
  163. {
  164.     WindowPtr    pWindow;
  165.     Rect        bounds;
  166.     short        totalMargin;
  167.     Point        newPosition;
  168.     GDHandle    hGDevice;
  169.  
  170.     pWindow = GetNewCWindow (windowRsrcID, nil, kWindowOnTop);
  171.     if (pWindow == nil)
  172.         return (nil);
  173.  
  174.     SetPort (pWindow);
  175.     GXIgnoreGraphicsNotice (transform_already_set);
  176.     SetDefaultViewPort (GXNewWindowViewPort (pWindow));
  177.     GXPopGraphicsNotice ();
  178.  
  179.     /* Find the deepest screen */
  180.     SetRect (&bounds, -32767, -32767, 32767, 32767);
  181.     hGDevice = GetMaxDevice (&bounds);
  182.     bounds = (**hGDevice).gdRect;
  183.  
  184.     totalMargin = (bounds.right - bounds.left) - (pWindow->portRect.right - pWindow->portRect.left);
  185.     newPosition.h = bounds.left + (totalMargin >> 1);
  186.  
  187.     totalMargin = (bounds.bottom - bounds.top) - (pWindow->portRect.bottom - pWindow->portRect.top - kWindowTitleHeight);
  188.     newPosition.v = bounds.top + ((totalMargin + ((hGDevice == GetMainDevice ()) ? GetMBarHeight() : 0)) >> 1);
  189.  
  190.     MoveWindow (pWindow, newPosition.h, newPosition.v, true);
  191.     ShowWindow (pWindow);
  192.  
  193.     gDragRect = qd.screenBits.bounds;
  194.     return (pWindow);
  195.  
  196. }    /* InitializeWindow */
  197.  
  198.  
  199.     void
  200. DoContentClick (EventRecord *pEvent, WindowPtr theWindow)
  201. {
  202.     if (theWindow == nil)
  203.         return;
  204.  
  205.     if (theWindow == FrontWindow ())
  206.     {
  207.         if (GetWindowKind (theWindow) == noGrowDocProc)
  208.         ;
  209.     }
  210.     else
  211.     {
  212.         SetPort (theWindow);
  213.         SelectWindow (theWindow);
  214.     }
  215.  
  216. }    /*    DoContentClick  */
  217.  
  218.  
  219.     void
  220. DoMenuCommand (long menuResult)
  221. {
  222.     short    menuID,
  223.             itemNumber;
  224.  
  225.     if (! menuResult)
  226.         return;
  227.  
  228.     menuID       = menuResult>>16;
  229.     itemNumber = menuResult & 0xFFFF;
  230.  
  231.     switch (menuID)
  232.     {
  233.         case appleMenuRsrcID:
  234.             switch (itemNumber)
  235.             {
  236.                 case itemAbout:
  237.                     Alert (aboutRsrcID, nil);
  238.                     break;
  239.  
  240.                 default:
  241. //                    DoDeskAcc (appleMenuRsrcID, itemNumber);
  242.                     SysBeep (1);
  243.                     break;
  244.             }
  245.             break;
  246.  
  247.         case fileMenuRsrcID:
  248.             switch (itemNumber)
  249.             {
  250.                 case itemQuit:
  251.                     gQuitApp = true;
  252.                     break;
  253.  
  254.                 default:
  255.                     SysBeep (1);
  256.                     break;
  257.             }
  258.             break;
  259.  
  260.         case animationMenuRsrcID:
  261.             switch (itemNumber)
  262.             {
  263.                 case itemBegin:
  264.                     gDoAnimation = true;
  265.                     EnableItem (GetMHandle (windowMenuRsrcID), itemCycle);
  266.                     break;
  267.  
  268.                 case itemEnd:
  269.                     gDoAnimation = false;
  270.                     DisableItem (GetMHandle (windowMenuRsrcID), itemCycle);
  271.                     break;
  272.  
  273.                 case itemStep:
  274.                     MoveObject ();
  275.                     break;
  276.             }
  277.             break;
  278.  
  279.         case objectMenuRsrcID:
  280.             switch (itemNumber)
  281.             {
  282.                 case itemSquare:
  283.                 case itemOval:
  284.                     gIsSquare = (itemNumber == itemSquare);
  285.                     CheckItem (GetMHandle (menuID), itemNumber, true);
  286.                     CheckItem (GetMHandle (menuID), gIsSquare ? itemOval : itemSquare, false);
  287.                     InitializeViewPorts (gWindow);
  288.                     InvalRect (&gWindow->portRect);
  289.                     break;
  290.  
  291.                 case itemDither:
  292.                     CheckItem (GetMHandle (menuID), itemNumber, gDoDither = !gDoDither);
  293.                     ChangeViewPorts (gWindow);
  294.                     InvalRect (&gWindow->portRect);
  295.                     break;
  296.  
  297.                 case itemHalftone:
  298.                     CheckItem (GetMHandle (menuID), itemNumber, gDoHalftone = !gDoHalftone);
  299.                     ChangeViewPorts (gWindow);
  300.                     InvalRect (&gWindow->portRect);
  301.                     break;
  302.             }
  303.             break;
  304.  
  305.         case windowMenuRsrcID:
  306.             if (itemNumber == itemShowFrame)
  307.                 CheckItem (GetMHandle (menuID), itemShowFrame, gShowFrame = ! gShowFrame);
  308.             else if (itemNumber == itemCycle)
  309.             {
  310.                 CheckItem (GetMHandle (menuID), itemCycle, gCyclePanes = ! gCyclePanes);
  311.                 if (gCyclePanes)
  312.                     GetNextCycle (&gNextCycle);
  313.             }
  314.             else
  315.             {
  316.                 long    oldItem;
  317.  
  318.                 CheckItem (GetMHandle (menuID), itemNumber, true);
  319.                 oldItem = gPanesPerSide - kMinimumPanesPerSide + itemMinimumPanes;
  320.                 CheckItem (GetMHandle (menuID), oldItem, false);
  321.                 gPanesPerSide = itemNumber - itemMinimumPanes + kMinimumPanesPerSide;
  322.             }
  323.             ChangeViewPorts (gWindow);
  324.             InvalRect (&gWindow->portRect);
  325.             break;
  326.  
  327.         default:
  328.             break;
  329.     }  /*  switch (menuID)  */
  330.  
  331.     HiliteMenu (kHiliteAllMenus);
  332.  
  333. }    /*    DoMenuCommand  */
  334.  
  335.  
  336.     void
  337. DoGrowBox (EventRecord *pEvent, WindowPtr theWindow)
  338. {
  339.     long        newSize;
  340.     Rect        newRect,
  341.                 oldRect;
  342.  
  343.     oldRect = newRect = theWindow->portRect;
  344.  
  345.     if (newSize = GrowWindow (theWindow, pEvent->where, &qd.screenBits.bounds))
  346.     {
  347.         newRect.right    = newRect.left + (short) newSize;
  348.         newRect.bottom    = newRect.top  + (short) (newSize >> 16);
  349.         SizeWindow (theWindow,
  350.                     newRect.right - newRect.left,
  351.                     newRect.bottom - newRect.top,
  352.                     true);
  353.         InvalRect (&oldRect);
  354.         ChangeViewPorts (gWindow);
  355.     }
  356.  
  357. }    /*    DoGrowBox  */
  358.  
  359.  
  360.     void
  361. DoZoomBox (EventRecord *pEvent, WindowPtr theWindow, short windowPart)
  362. {
  363.     if (TrackBox (theWindow, pEvent->where, windowPart))
  364.     {
  365.         ZoomWindow (theWindow, windowPart, true);
  366.         InvalRect (&theWindow->portRect);
  367.         ChangeViewPorts (gWindow);
  368.     }
  369.  
  370. }    /*    DoZoomBox  */
  371.  
  372.  
  373.     void
  374. DoNullEvent (EventRecord *pEvent)
  375. {
  376.     if (gDoAnimation)
  377.     {
  378.         if (gCyclePanes)
  379.         {
  380.             if (GetNextCycle (&gNextCycle))
  381.             {
  382.                 gPanesPerSide = (gPanesPerSide == kMaximumPanesPerSide)    ? kMinimumPanesPerSide
  383.                                                                         : gPanesPerSide + 1;
  384.                 ChangeViewPorts (gWindow);
  385.                 InvalRect (&gWindow->portRect);
  386.             }
  387.         }
  388.         MoveObject ();
  389.     }
  390.  
  391. }    /*    DoNullEvent  */
  392.  
  393.  
  394.     void
  395. DoMouseDown (EventRecord *pEvent)
  396. {
  397.     short        windowPart;
  398.     WindowPtr    whichWindow;
  399.  
  400.     windowPart = FindWindow (pEvent->where, &whichWindow);
  401.  
  402.     switch (windowPart)
  403.     {
  404.     case inDesk:
  405.         break;
  406.  
  407.     case inMenuBar:
  408.         DoMenuCommand (MenuSelect (pEvent->where));
  409.         break;
  410.  
  411.     case inSysWindow:
  412.         SystemClick (pEvent, whichWindow);
  413.         break;
  414.  
  415.     case inContent:
  416.         DoContentClick (pEvent, whichWindow);
  417.         break;
  418.  
  419.     case inDrag:
  420.         DragWindow (whichWindow, pEvent->where, &gDragRect);
  421.         break;
  422.  
  423.     case inGrow:
  424.         DoGrowBox (pEvent, whichWindow);
  425.         break;
  426.  
  427.     case inGoAway:
  428.           if (TrackGoAway (whichWindow, pEvent->where))
  429.           {
  430.               if (whichWindow == gWindow)
  431.                 gQuitApp = true;
  432.             else
  433.                 DebugStr ("\pDoMouseDown: Clicked in window of unknown close box");
  434.           }
  435.         break;
  436.  
  437.     case inZoomIn:
  438.     case inZoomOut:
  439.         DoZoomBox (pEvent, whichWindow, windowPart);
  440.         break;
  441.  
  442.     default:
  443.         break;
  444. }    /* switch */
  445.  
  446. }    /*    DoMouseDown  */
  447.  
  448.  
  449.     void
  450. DoKeyStroke (EventRecord *pEvent)
  451. {
  452.     char    charCode;
  453.  
  454.     charCode = pEvent->message & charCodeMask;
  455.  
  456.     if (pEvent->modifiers & btnState)
  457.     {
  458.         /*--------------*/
  459.         /* Button is UP */
  460.         /*--------------*/
  461.         if (pEvent->modifiers & cmdKey)
  462.         {
  463.             /*-------------------*/
  464.             /* Command key    x100 */
  465.             /*-------------------*/
  466.             DoMenuCommand (MenuKey (charCode));
  467.         }
  468.     }
  469.  
  470. }    /*    DoKeyStroke  */
  471.  
  472.  
  473.     void
  474. DoUpdate (EventRecord *pEvent)
  475. {
  476.     GrafPtr        savedPort;
  477.     WindowPtr    pUpdateWindow;
  478.  
  479.     GetPort (&savedPort);
  480.  
  481.     pUpdateWindow = (WindowPtr) pEvent->message;
  482.  
  483.     SetPort (pUpdateWindow);
  484.     BeginUpdate (pUpdateWindow);
  485.  
  486.     EraseRgn (pUpdateWindow->visRgn);
  487.  
  488.     if (pUpdateWindow == gWindow)
  489.     {
  490.         if (gShowFrame)
  491.             DrawObjectFrame ();
  492.         DrawObject ();
  493.     }
  494.  
  495.     EndUpdate (pUpdateWindow);
  496.     SetPort (savedPort);
  497.  
  498. }    /*    DoUpdate  */
  499.  
  500.  
  501.     void
  502. DoActivate (EventRecord    *pEvent)
  503. {
  504.     WindowPtr    pActiveWindow;
  505.  
  506.     /*--------------------------------------*/
  507.     /* Get the window to de/activate        */
  508.     /* and its kind from the event message    */
  509.     /*--------------------------------------*/
  510.     pActiveWindow = (WindowPtr) pEvent->message;
  511.  
  512.     SetPort (pActiveWindow);
  513.  
  514.     /*-----------------*/
  515.     /* Activate window */
  516.     /*-----------------*/
  517.     if (pEvent->modifiers & activeFlag)
  518.     {
  519.         /* Enable/Disable items */
  520.     }
  521.     else
  522.     /*-------------------*/
  523.     /* Deactivate window */
  524.     /*-------------------*/
  525.     {
  526.         /* Enable/Disable items */
  527.     }
  528.  
  529. }    /*    DoActivate  */
  530.  
  531.  
  532.     void
  533. DoEvent  (void)
  534. {
  535.     EventRecord    theEvent;
  536.  
  537.     SystemTask ();        /* Handle desk accessories */
  538.  
  539.     GetNextEvent (everyEvent, &theEvent);
  540.  
  541.     switch (theEvent.what)
  542.     {
  543.     case nullEvent:
  544.         DoNullEvent (&theEvent);
  545.         break;
  546.  
  547.     case mouseDown:
  548.         DoMouseDown (&theEvent);
  549.         break;
  550.  
  551.     case keyDown:
  552.     case autoKey:
  553.         DoKeyStroke (&theEvent);
  554.         break;
  555.  
  556.     case updateEvt:
  557.         DoUpdate (&theEvent);
  558.         break;
  559.  
  560.     case activateEvt:
  561.         DoActivate (&theEvent);
  562.         break;
  563.  
  564.     case mouseUp:
  565.     case keyUp:
  566.     case diskEvt:
  567.     case networkEvt:
  568.     case driverEvt:
  569.     default:
  570.         break;
  571.     }  /* switch (theEvent.what)  */
  572.  
  573. }    /* DoEvent */
  574.  
  575.  
  576. main (void)
  577. {
  578.     gQuitApp = false;
  579.     if (InitApp ())
  580.     {
  581.         InitializeViewPortGlobals ();
  582.  
  583.         InitializeMenus ();
  584.         if (gWindow = InitializeWindow ())
  585.         {
  586.             InitializeViewPorts (gWindow);
  587.  
  588.             while (! gQuitApp)
  589.                 DoEvent ();
  590.  
  591.             DisposeViewPorts (gWindow);
  592.         }
  593.         ExitApp ();
  594.     }
  595.  
  596. }    /* main */
  597.